<?php
namespace houze;

/**
 * 客户端请求类 
*/ 
class Curl{
    /**
     * 单例对象
     */ 
    protected static $instance = null;
    /**
     * Cookie存储 
     */ 
    protected $cookie = null;
    /**
     * 数据存储
     */ 
    protected $data = null;
    /**
     * 配置存储
     */ 
    protected $set = [];
    /**
     * 请求URL存储
     */ 
    protected $url = '';
    /**
     * 来路页面存储
     */
    protected $domain = '';
    /**
     * 请求类型存储
     */ 
    protected $type = null;
    /**
     * 请求头存储
     */ 
    protected $header = null;
    /**
     * 是否获取请求头
     */ 
    protected $isHeader = false;
    /**
     * UserAgent存储
     */ 
    protected $userAgent = null;
    /**
     * 存储编码格式
     */ 
    protected $encode = null;
    /**
     * 请求超时时间设置
     */ 
    protected $outTime = 5;
    /**
     * 是否开启SSL请求
     */ 
    protected $ssl = false;
    /**
     * 是否开启RAW请求
     */ 
    protected $raw = false;
    /**
     * 存储客户端
     */ 
    protected $client = null;
    
    /**
     * 初始化
     */ 
    public function __construct(){
        // 初始化客户端
        $this->client = curl_init();
    }
    
    // 获取客户端返回信息
    public function getInfo(){
        return curl_getinfo($this->client);
    }
    
    // 获取配置信息
    public function getSet(){
        return $this->set;
    }
    
    // 设置配置信息
    public function set(){
        // 是否输出响应头
        $this->set[CURLOPT_HEADER] = $this->isHeader ? true : false;
        // 是否获取请求头
        $this->set[CURLINFO_HEADER_OUT] = $this->isHeader ? true : false;
        // 超时(秒)
        $this->set[CURLOPT_TIMEOUT] = $this->outTime;
        // 输出数据流
        $this->set[CURLOPT_RETURNTRANSFER] = $this->raw ? false : true;
        // 认证证书检查
        $this->set[CURLOPT_SSL_VERIFYPEER] = $this->ssl ? true : false;
        // 检查SSL加密算法
        $this->set[CURLOPT_SSL_VERIFYHOST] = $this->ssl ? 2 : false;
        
        switch ($this->type) {
            case 'GET':
                if(!empty($this->data)){
                    $this->url .= '?';
                    $this->url .= http_build_query($this->data);
                    $this->set[CURLOPT_URL] = $this->url;
                }
                break;
            case 'POST':
                $this->set[CURLOPT_POST] = true;
                $this->set[CURLOPT_POSTFIELDS] = $this->data;
                break;
            case 'PUT':
                $this->set[CURLOPT_CUSTOMREQUEST] = 'PUT';
                $this->set[CURLOPT_POSTFIELDS] = $this->data;
                break;
            case 'DELETE':
                $this->set[CURLOPT_CUSTOMREQUEST] = 'DELETE';
                $this->set[CURLOPT_POSTFIELDS] = $this->data;
                break;
            case 'UPOST':
                $this->set[CURLOPT_POST] = true;
                $this->set[CURLOPT_POSTFIELDS] = http_build_query($this->data);
                break;
            case 'UPUT':
                $this->set[CURLOPT_CUSTOMREQUEST] = 'PUT';
                $this->set[CURLOPT_POSTFIELDS] = http_build_query($this->data);
                break;
            case 'UDELETE':
                $this->set[CURLOPT_CUSTOMREQUEST] = 'DELETE';
                $this->set[CURLOPT_POSTFIELDS] = http_build_query($this->data);
                break;
        }

        // 应用配置
        curl_setopt_array($this->client, $this->set);
        
        return $this;
    }
    
    // 设置UserAgent值
    public function userAgent($userAgent){
        $this->userAgent = $userAgent;
        $this->set[CURLOPT_USERAGENT] = $this->userAgent;
        return $this;
    }
    
    // 设置Cookie值
    public function cookie($cookie){
        $this->cookie = $cookie;
        $this->set[CURLOPT_COOKIE] = $this->cookie;
        return $this;
    }
    
    // 设置URL值
    public function url($url){
        $this->url = $url;
        $this->set[CURLOPT_URL] = $this->url;
        return $this;
    }
    
    // 是否获取请求头
    public function isHeader($header = true){
        $this->isHeader = $header;
        return $this;
    }
    
    // 设置请求头
    public function header($header){
        $this->header = $header;
        $this->set[CURLOPT_HTTPHEADER] = $this->header;
        return $this;
    }
    
    // 设置代理
    public function proxy($ip, $port, $username_password = null){
        $this->set[CURLOPT_PROXY] = $ip;
        $this->set[CURLOPT_PROXYPORT] = $port;
        if(!empty($username_password)) $this->set[CURLOPT_PROXYUSERPWD] = $username_password;
        return $this;
    }
    
    // 设置编码格式
    public function encode($encode){
        $this->encode = $encode;
        $this->set[CURLOPT_ENCODING] = $this->encode;
        return $this;
    }
    
    // 设置请求超时时间
    public function outTime($time){
        $this->outTime = $time;
        return $this;
    }
    
    // 伪装来路页面
    public function domain($domain){
        $this->domain = $domain;
        $this->set[CURLOPT_REFERER] = $this->domain;
        return $this;
    }
    
    // 设置数据
    public function data($data){
        $this->data = $data;
        return $this;
    }
    
    // 是否开启SSL
    public function ssl($set = true){
        $this->ssl = $set;
        return $this;
    }
    
    // 是否开启RAW
    public function raw($set = true){
        $this->raw = $set;
        return $this;
    }
    
    // 发送GET请求
    public function get(){
        $this->type = 'GET';
        $this->set();
        return curl_exec($this->client);
    }
    
    // 发送POST请求
    public function post(){
        $this->type = 'POST';
        $this->set();
        return curl_exec($this->client);
    }
    
    // 发送UPOST请求
    public function upost(){
        $this->type = 'UPOST';
        $this->set();
        return curl_exec($this->client);
    }
    
    // 发送PUT请求
    public function put(){
        $this->type = 'PUT';
        $this->set();
        return curl_exec($this->client);
    }
    
    // 发送UPUT请求
    public function uput(){
        $this->type = 'UPUT';
        $this->set();
        return curl_exec($this->client);
    }
    
    // 发送DELETE请求
    public function delete(){
        $this->type = 'DELETE';
        $this->set();
        return curl_exec($this->client);
    }
    
    // 发送UDELETE请求
    public function udelete(){
        $this->type = 'UDELETE';
        $this->set();
        return curl_exec($this->client);
    }
    
    /**
     * 垃圾回收
     */ 
    public function __destruct(){
        // 归还客户端
        curl_close($this->client);
    }
}